//
//  ST_PecSquareBestLoveSectionCell.swift
//  btvideo
//
//  Created by mac on 2020/6/4.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import SnapKit

class ST_PecSquareBestLoveSectionCell: UICollectionViewCell {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.addSubview(left_icon_image_view)
        contentView.addSubview(titleLabel)
        contentView.addSubview(more_data_btn)
        
        contentView.addSubview(clip_view)
        clip_view.addSubview(scrollView)
        
        //        contentView.backgroundColor = Color.random
        
        left_icon_image_view.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(5)
            make.left.equalToSuperview().offset(15)
            make.width.height.equalTo(16)
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(left_icon_image_view.snp.right).offset(5)
            make.centerY.equalTo(left_icon_image_view)
        }
        
        
        more_data_btn.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-15)
            make.centerY.equalTo(left_icon_image_view)
            make.width.equalTo(75)
            make.height.equalTo(25)
        }
        
        clip_view.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.height.equalTo(65*3+15*2)
            make.top.equalTo(more_data_btn.snp.bottom).offset(10)
        }
        
        scrollView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.init(top: 0, left: 5, bottom: 0, right: 25))
        }
        
        scrollView.addSubview(scrollViewContainer)
        scrollViewContainer.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
            make.width.equalTo(300)
            make.height.equalToSuperview()
        }
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //    MARK: - Properties
    
    fileprivate lazy var left_icon_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        return control
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#333333")
        //        control.numberOfLines = 0
        //        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var more_data_btn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.backgroundColor = Color.init(hexString: "#F2F4F7")
        control.cornerRadius = 12
        control.setTitleForAllStates("查看全部")
        control.setTitleColorForAllStates(UIColor.init(hexString: "#333333")!)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.semibold)
        control.addTarget(self, action: #selector(push_more_data_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate lazy var scrollView:UIScrollView = {
       let control = UIScrollView()
        control.isPagingEnabled = true
        control.clipsToBounds = false
        control.showsHorizontalScrollIndicator = false
        control.showsVerticalScrollIndicator = false
        return control
    }()
    
    fileprivate lazy var scrollViewContainer:UIView = {
        let control = UIView()
        return control
    }()
    
//    controlLayout.itemSize = CGSize.init(width: ScreenWidth-15-25+10, height: 65)
    
    
    fileprivate lazy var clip_view:UIView = {
       let control = UIView()
        control.clipsToBounds = true
        return control
    }()
    
    //    MARK: - Bind Model
    var model = ST_PeculiaritySquareCollectionCellModel(){
        didSet{
            
            left_icon_image_view.image = UIImage.init(named: model.iconImageName)
            
            titleLabel.text = model.title
            
            reload_data_child_view()
            
        }
    }
}

//MARK: - Event
extension ST_PecSquareBestLoveSectionCell{
    
    @objc func push_more_data_handle(){
        
        let vc = ST_PeculiaritySquareTypeListController()
        
        vc.model = self.model
        
        self.parentViewController?.navigationController?.pushViewController(vc)
        
    }
}


//MARK:- UICollectionViewDelegate
extension ST_PecSquareBestLoveSectionCell{
    
    
    func reload_data_child_view(){
        
        scrollViewContainer.removeSubviews()
        
        let w = ScreenWidth-15-25+10
        
        let 总列数 = (model.array.count.float/3.0).ceil
        
        scrollViewContainer.snp.updateConstraints { (make) in
            make.width.equalTo(w*总列数.cgFloat)
        }
        
//        print("总列数:",总列数)
        
        for (idx,item) in model.array.enumerated() {
            
            
            let 列 = ((idx.float + 1) / 3.0).ceil
            let 行 = idx%3
            
//            print("❌错误---行:",行,"----列：",列)
            
            let cell = PecSquareBestLoveSectionChildCell()
            
            scrollViewContainer.addSubview(cell)
            
            let x = (列-1).cgFloat*w
            
            let y = 行.cgFloat*(65+15)
            
            cell.snp.makeConstraints { (make) in
                make.width.equalTo(ScreenWidth-15-25+10)
                make.height.equalTo(65)
                make.left.equalToSuperview().offset(x)
                make.top.equalToSuperview().offset(y)
            }
            
            cell.child_model = item
            
            cell.display_own_number = false
        }
        
    }
    
}

//MARK:- Child Cell

fileprivate class PecSquareBestLoveSectionChildCell: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(cover_image_view)
        self.addSubview(titleLabel)
        self.addSubview(content_label)
        
        
        cover_image_view.snp.makeConstraints { (make) in
            make.top.bottom.equalToSuperview()
            make.left.equalToSuperview().offset(10)
            make.width.height.equalTo(65)
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(cover_image_view).offset(11)
            make.left.equalTo(cover_image_view.snp.right).offset(15)
            make.height.equalTo(22)
        }
        
        content_label.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(4)
            make.left.equalTo(titleLabel)
            make.height.equalTo(17)
        }
        
        
        self.addSubview(add_btn)
        add_btn.snp.makeConstraints { (make) in
            make.right.equalToSuperview()
            make.centerY.equalToSuperview()
            make.width.equalTo(74)
            make.height.equalTo(31)
        }
        
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(push_pec_handle))
     
        self.addGestureRecognizer(tap)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //    MARK: - Properties
    
    fileprivate lazy var cover_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        control.cornerRadius = 15
        return control
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#333333")
        //        control.numberOfLines = 0
        //        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#ACB4C0")
        //        control.numberOfLines = 0
        //        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
        return control
    }()
    
    
    fileprivate lazy var add_btn:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setTitleForAllStates("添加")
        control.titleLabel?.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.semibold)
        control.setTitleColorForAllStates(UIColor.white)
        control.cornerRadius = 15
        control.backgroundColor = UIColor.init(hex: 0xFEC207)
        control.addTarget(self, action: #selector(add_click_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    //    MARK:-
    var child_model = ST_PeculiarityModel(){
        didSet{
                        
            cover_image_view.pin_setImage(from: child_model.image.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            
            titleLabel.text = child_model.name
            
//            content_label.text =  child_model.own_num.string + "个" + (child_model.type == 1 ? "男生" : "女生")
            
        }
    }
    
    
    var display_own_number = true{
        didSet{
            if display_own_number{
                content_label.text =  child_model.own_num.string + "个" + (child_model.type == 1 ? "男生" : "女生")
            }else{
                content_label.text =  child_model.like_num.string + "个" + (child_model.type != 1 ? "男生" : "女生")
            }
        }
    }
    
    
    
    @objc fileprivate func add_click_handle(){
        
        
        push_peculiarity(pecId: child_model.id.string, navigationController: self.parentViewController?.navigationController, hud_controller: self.parentViewController)
        
    }
    
    @objc fileprivate func push_pec_handle(){
        
        push_peculiarity(pec: child_model, navigationController: self.parentViewController?.navigationController)
    }
}
